<template>
  <el-card class="box-card">
    <div>
      <!-- 搜索框 -->
      <el-form>
        <el-form-item>
          <el-button type="danger" @click="openInsertWindow">新增</el-button>
        </el-form-item>
      </el-form>
      <!-- 角色数据表单 -->
      <el-table :data="dataList" border>
        <el-table-column label="公告编号" prop="noticeId" width="150">
        </el-table-column>
        <el-table-column label="公告标题" prop="title" width="200">
        </el-table-column>
        <el-table-column label="公告内容" prop="content" width="500">
        </el-table-column>
        <el-table-column label="发布时间" prop="time" width="160">
        </el-table-column>
        <el-table-column label="图标" prop="icon" width="50">
          <template v-slot="scope">
            <i :class="`el-icon-${scope.row.icon}`"></i>
          </template>
        </el-table-column>
      </el-table>
      <!-- 新增按钮打开的页面 -->
      <el-dialog
        :visible.sync="insertWindow"
        title="新增系统公告"
      >
        <el-form :model="insertData">
          <el-form-item label="公告标题" label-width="120px">
            <el-input
              v-model="insertData.title"
              autocomplete="off"
              placeholder="系统标题"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="公告内容" label-width="120px">
            <el-input
              v-model="insertData.content"
              autocomplete="off"
              placeholder="公告内容"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="公告图标" label-width="120px">
            <el-select v-model="insertData.icon" placeholder="请选择">
              <el-row style="width: 300px;">
                <el-col
                  v-for="item in icons"
                  :key="item"
                  :span="6"
                >
                  <el-option
                    v-for="{icon,name} in item"
                    :key="icon"
                    :label="name"
                    :value="icon"
                    class="el-option"
                  >
                    <i :class="`el-icon-${icon}`"/>
                  </el-option>
                </el-col>
              </el-row>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="insertWindow = false">取 消</el-button>
          <el-button type="primary" @click="insertNotice()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 页号 -->
      <el-pagination
        :current-page="pageIndex"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="totalPage"
        layout="total ,sizes,prev,pager,next,jumper"
        style="margin-top: 30px"
        @size-change="sizeChangeHandle"
        @current-change="CurrentChangeHandle"
      >
      </el-pagination>
    </div>
  </el-card>
</template>
<script>

export default {
  name: "SysNotices",
  data() {
    return {
      //数据参数
      insertData: {},//新增数据

      //集合参数
      icons: [], //图标集合
      dataList: [], //页面展示的数据集合

      //页号参数
      pageIndex: 1,
      pageSize: 5,
      pageSizes: [],
      totalPage: 0,
      begin: 0,
      end: this.pageSize - 1,

      //窗口显示参数
      insertWindow: false,
    };
  },
  methods: {
    //改变数组大小
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.getDataList();
    },
    //改变当前页号
    CurrentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },

    //分页操作
    cutDataList(res) {
      const data = this.$cutPageDataList(res)
      this.dataList = data.dataList
      this.totalPage = data.totalPage
    },

    //查询所有数据
    getDataList() {
      const pageInfo = this.$getPageInfo(this.pageSize, this.pageIndex)
      this.$http.post("/notice/getPageVo", pageInfo).then((res) => {
        this.cutDataList(res)
      });
    },
    //获取公告图标字典
    getIcon() {
      this.$http.post("/dict/getList/icon").then(res => {
        this.icons = this.$StrToJson(res);

        const len = this.icons.length //数组长度
        const num = 4 //数组数量
        const arr = []
        for (let i = 0; i < len; i++) {
          if (i < num) {
            arr.push([])
          }
          arr[i % num].push(this.icons[i])
        }
        this.icons = arr
      })
    },

    //添加公告
    insertNotice() {
      //关闭窗口
      this.insertWindow = false;
      const data = {
        noticesBo: this.insertData,
        pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
      }
      this.$http.post("/notice/insert", data).then((res) => {
        this.cutDataList(res);
      });
    },

    //打开新增页面
    openInsertWindow() {
      this.insertWindow = true;
      this.insertData = {};
    },

    //初始化页号
    setPageSize() {
      this.pageSizes = this.$getPageSize()
      this.pageSize = this.pageSizes[0]
    }
  },
  mounted() {
    this.setPageSize()
    this.getIcon();
    this.getDataList();
  },
};
</script>
